home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-04-20 | 7.9 KB | 305 lines |
- package server;
-
- import java.io.*;
- import java.net.*;
-
- /**
- * The DataServer processes requests from clients to access the
- * database.
- * The DataServer runs until you hit control-c. It is a multithreaded
- * server and spins off threads to process each connection from
- * a client.
- * @see server.DB
- * @version 1.0
- * @author Barry Boone
- */
- public class Server {
-
- /** opcode for making a reservation. */
- public static final int OP_MAKE_RESERVATION = 1;
-
- /** opcode for deleting a reservation. */
- public static final int OP_DELETE = 2;
-
- /** opcode for getting a passenger's seat. */
- public static final int OP_GET_SEAT = 3;
-
- /** opcode for getting all open seats. */
- public static final int OP_GET_OPEN_SEATS = 4;
-
- /** opcode for getting all passengers. */
- public static final int OP_GET_PASSENGER_LIST = 5;
-
- private int port = 5001;
- private boolean listening = true;
- private DB db;
- private ServerSocket serverSock;
-
- /** Start the database server spinning. */
- public static void main(String args[]) {
- int portnum = 5001;
-
- System.out.println("The Server runs until you hit control-c");
-
- try {
- Server server = new Server();
- server.start();
- } catch (IOException x) {
- System.out.println("Could not create server socket: " +
- x.getMessage());
- System.exit(1);
- }
- }
-
- /**
- * Put the Server object into action.
- * @exception IOException thrown if we cannot create a ServerSocket
- * bound to the specified port
- */
- public void start() throws IOException {
-
- serverSock = null;
- Socket sock;
-
- db = new DB();
-
- try {
- serverSock = new ServerSocket(port, 50);
-
- while (listening) {
-
- // Wait for a client to connect with the server socket.
- // Then spin off a thread to handle the connection
- // with the client, repeat.
- try {
- // Wait here until contacted by a client.
- sock = serverSock.accept();
-
- new ClientServerThread(sock, db).start();
- } catch (IOException e) {
- System.out.println("Connection dropped?: " +
- e.getMessage());
- }
- }
-
- } finally {
- serverSock.close();
- serverSock = null;
- }
- }
-
- protected void finalize() throws Throwable {
- super.finalize();
- if (serverSock != null) {
- serverSock.close();
- serverSock = null;
- }
- }
- }
-
- // Handles one transaction with a client.
- class ClientServerThread extends Thread {
-
- private static final int SUCCESS = 0;
- private static final int FAILURE = 1;
-
- private static final int PAUSE_FOR = 50;
-
- private DB db;
- private Socket sock;
- private DataInputStream remoteIn;
- private DataOutputStream remoteOut;
-
- ClientServerThread(Socket sock, DB db) {
- this.sock = sock;
- this.db = db;
- }
-
- // Ths run() method processes just one request from a client and
- // then ends.
- public void run() {
- try {
- remoteIn = new DataInputStream(sock.getInputStream());
- remoteOut = new DataOutputStream(sock.getOutputStream());
-
- // Get the opcode so we know how to handle this transaction.
- int opcode = remoteIn.readInt();
-
- switch (opcode) {
- case (Server.OP_MAKE_RESERVATION):
- makeReservation();
- break;
- case (Server.OP_DELETE):
- delete();
- break;
- case (Server.OP_GET_SEAT):
- getSeat();
- break;
- case (Server.OP_GET_OPEN_SEATS):
- getOpenSeats();
- break;
- case (Server.OP_GET_PASSENGER_LIST):
- getPassengerList();
- break;
- default:
- done();
- return;
- }
-
- } catch (IOException e) {
- error(e);
- }
- }
-
- // Make a reservation.
- // in: <opcode> <passenger name> <seat number>
- // out: <0 OR 1 (success or failure)>
- private void makeReservation() {
- try {
- String name = remoteIn.readUTF();
- int seat = remoteIn.readInt();
- db.reservePassenger(name, seat);
- sendToClient(SUCCESS);
- } catch (SeatTakenException x) {
- sendToClient(FAILURE);
- } catch (IOException x) {
- System.out.println(x.getMessage());
- sendToClient(FAILURE);
- } finally {
- done();
- }
- }
-
- // Delete a reservation.
- // in: <opcode> <passenger name>
- // out: <0 OR 1 (success or failure)>
- private void delete() {
- try {
- String name = remoteIn.readUTF();
- db.deletePassenger(name);
- sendToClient(SUCCESS);
- } catch (IOException e) {
- error(e);
- sendToClient(FAILURE);
- } finally {
- done();
- }
- }
-
- // Get a seat given a passenger.
- // in: <opcode> <passenger name>
- // out: <seat number> (-1 if no seat found)
- private void getSeat()
- {
- try {
- String name = remoteIn.readUTF();
- int seat = db.getSeat(name);
- sendToClient(seat);
- } catch (Exception x) {
- sendToClient(FAILURE);
- } finally {
- done();
- }
- }
-
- // Get all open seats.
- // in: <opcode>
- // out: <number of open seats> { <seat number> }
- private void getOpenSeats()
- {
- try {
- int[] seats = db.getOpenSeats();
- sendToClient(seats.length);
- for (int i = 0; i < seats.length; i++)
- sendToClient(seats[i]);
- } catch (Exception x) {
- } finally {
- done();
- }
- }
-
- // Get passenger list.
- // in: <opcode>
- // out: <number of passengers> { <passenger name> }
- private void getPassengerList()
- {
- try {
- String[] passengers = db.getPassengerList();
- sendToClient(passengers.length);
- for (int i = 0; i < passengers.length; i++)
- sendToClient(passengers[i]);
- } catch (Exception x) {
- } finally {
- done();
- }
- }
-
- // Close the connection with the client.
- private void done() {
- try {
- if (remoteOut != null) {
- remoteOut.close();
- remoteOut = null;
- }
-
- if (remoteIn != null) {
- remoteIn.close();
- remoteIn = null;
- }
- } catch (IOException x) {
- error(x);
- } finally {
- try {
- if (sock != null) {
- sock.close();
- sock = null;
- }
- } catch (IOException x) {
- error(x);
- }
- }
- }
-
- private void sendToClient(int i) {
- try {
- remoteOut.writeInt(i);
- } catch (IOException x) {
- error(x);
- }
-
- pause();
- }
-
- private void sendToClient(String s) {
- try {
- remoteOut.writeUTF(s);
- } catch (IOException x) {
- error(x);
- }
-
- pause();
- }
-
- // On Windows95, at least, when the client and server are running on
- // the same machine, we have to pause for the client to read
- // the data when the server writes it to the socket.
- // Otherwise, the client hangs.
- private void pause() {
- try {
- sleep(PAUSE_FOR);
- } catch (InterruptedException x) {
- error(x);
- }
- }
-
- private void error(Exception x) {
- System.out.println("Connection dropped?: " + x.getMessage());
- }
-
- protected void finalize() throws Throwable {
- super.finalize();
- done();
- }
-
- }
-